<?php
namespace app\index\controller;
use think\Request;
use think\Db;
use think\Controller;

use app\index\model\User as User;
use app\index\model\Menu as Menumodel;
use think\Validate;
use think\Session;


class Index extends Controller
{
    protected $user;
    protected $info = [];

	  public function check1(Request $req)//用户名
    {
     
        $user = new User;
        if(!empty($req->post('user'))){
            $username = $req->param('user');
            if(!preg_match('/^[a-zA-Zxa0-xff_][0-9a-zA-Zxa0-xff_]{3,15}$/',$username)){
              return false;
            }else{
              $result = $user->check($username);
              return $result;
            }
        }else{
          return  false;
        }
    }
    public function check2(Request $req)//邮箱
    {
      
       if(!empty($req->post('email') ) ){//获取邮箱
            $email = $req->param('email');
            $pattern = "/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i";
            if(!preg_match($pattern,$email)) {
             return false;
          }else{
            return true;
          }
        
       }else{
          return false;
       }
    }
    public function check3(Request $req)//密码
    {
       if(!empty($req->post('password') ) ){//获取密码
           $password = $req->param('password');
     
        $pattern = '/^[a-zA-Z\d_]{8,}$/';       //;
        if(!preg_match($pattern,$password)) {
              return false;
           }else{
              return true;
           }
       }      
    }
    public function check4(Request $req)//验证码
    {

      $phone = $req->param('phone');
      return true; 
    }
     public function regs(Request $request)//注册
    {
      
      $user = new User;
      $username = trim($request->post('user'));
      $email = $request->post('email');
      $password = md5($request->post('password'));
     // $phone = $request->post('phone');
      $data = [
        'username'   => $username,
        'password'   => $password,
        'email'      => $email,
      ];
      $result = $user->reg($data);
      if($result){
        session('username',$username);
        //session('picture',$picture);
        session('password',$password);
        $this->email($email,$username,$password);
        
      }else{
        return false;
      }
    }
    public function loging(Request $request)//登录
    {
      $user = new User;
      $username = $request->param('username');
      //return $username;
     
      $result = $user->logings($username);
      if($result){
        $picture = $result['picture'];
        $password = $result['password'];
        session('username',$username);
        session('picture',$picture);
        session('password',$password);
      }

    }
    public function out(Request $requset)//退出
    {
      $data = session('username',null);
      if($data){
        return false;
      }else{
        return true;
      }

    }
    public function index(menuModel $mol)//首页
    {
      $menu = $mol->menu();
      $this->assign('menu',$menu);
      $this->assign('username',session('username'));
      return $this->fetch();

    }
    public function menued(Request $req ,menuModel $mol)//查询菜单
    {
     $id = $req->param('id');

     return $mol->selemenu($id);
     //dump(json_encode($mol->selemenu($id)));
    }

    public function about( )
    {
		return $this->fetch();
    }

    public function blog( )
    {
		return $this->fetch();
    }

    public function faq( )
    {
	     	return $this->fetch();
    }

 	  public function payment( )
    {
		     return $this->fetch();
    }
    //短信验证
    //public function index()
    //{
   //    $options['accountsid']='55b4800138b28f1066e15272a16702aa';
   //    $options['token']='350e1abe59190a09f0f8112f10b51392';
   //    $phpone = new \my\Phone($options);
   //     $appId = "210fd2a99496497caca8d49475bba568";//当前应用
   //     $to = "15030893082";
   //     $templateId = "44080";//模板ID
   //     $param="3214";
      
   //    /* $this->weather();天气 */
   //    /*echo $phpone->templateSMS($appId,$to,$templateId,$param);
   //    return $this->fetch();*/
    // }
    
    public function yz(Request $req)//邮箱验证
    {
      $mima = $req->param('code');//截取32
      $mima = substr($mima,32);
      $name = $req->param('key');//获取名字
     // $this->success('激活成功','index/index/index');
      $this->assign("code",$mima);
      $this->assign("key",$name);
      return $this->fetch();

    }
    public function concheck(Request $request)//邮箱激活
    {
      $code = $request->param('code');//密码匹配
      $username = trim($request->param('name'));//数据库修改用ID
      $password = md5($request->param('password'));
      if($password == $code){
        $this->success('激活成功','index/index/index');
      }else{
        $this->error('激活失败');
      }
    

    }
    //邮箱发送消息
    public function email($email="675687922@qq.com",$username="asd",$password="asd")
    {
      $value = md5(mt_rand(0,9)).$password;
      $mail = new \mal\Email();
      $mail->setServer("smtp.exmail.qq.com", "shangcheng@bbszone.cn", "Zbs9171");// 设置smtp服务器
      $mail->setFrom("shangcheng@bbszone.cn"); //设置发件人 
      $mail->setReceiver("$email"); //设置收件人，多个收件人，调用多次  $email
     //$mail->setCc("XXXX"); //设置抄送，//多个抄送，调用多次
     //$mail->setBcc("XXXXX"); //设置秘密抄送，多个秘密抄送，调用多次
      $mail->setMailInfo("test", "<p>亲爱的，$email</p><p>感谢您申请注册贝缇丽思! 请点击链接完成注册</p><a href='http://www.best.com/index/index/yz?key=$username&code=$value'>立即激活</a>"); //设置邮件主题、内容
      return  $mail->sendMail(); //发送

    }
    public function weather()//获取天气
    {
          $host = "http://jisutqybmf.market.alicloudapi.com";
          $path = "/weather/query";
          $method = "GET";
          $appcode = "099e286e9e1d4474a82ecc7c76ce11de";
          $headers = array();
          array_push($headers, "Authorization:APPCODE " . $appcode);
          $querys = "city=%E5%AE%89%E9%A1%BA&citycode=citycode&cityid=cityid&ip=ip&location=location";
          $bodys = "";
          $url = $host . $path . "?" . $querys;

          $curl = curl_init();
          curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
          curl_setopt($curl, CURLOPT_URL, $url);
          curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
          curl_setopt($curl, CURLOPT_FAILONERROR, false);
          curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
          curl_setopt($curl, CURLOPT_HEADER, true);
          if (1 == strpos("$".$host, "https://"))
          {
              curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
              curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
          }
          $data = curl_exec($curl);
          $re2 = substr($data,strpos($data,'{'));
 
           $res = json_decode($re2);
           //dump($res->result->data);
           $result = $res->result;
           var_dump($result);
    }
    /*public function citys()//城市
    {
            $host = "http://jisutqybmf.market.alicloudapi.com";
            $path = "/weather/city";
            $method = "GET";
            $appcode = "099e286e9e1d4474a82ecc7c76ce11de";
            $headers = array();
            array_push($headers, "Authorization:APPCODE " . $appcode);
            $querys = "";
            $bodys = "";
            $url = $host . $path;

            $curl = curl_init();
            curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($curl, CURLOPT_FAILONERROR, false);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_HEADER, true);
            if (1 == strpos("$".$host, "https://"))
            {
                curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
            }
           $data = curl_exec($curl);
           $re2 = substr($data,strpos($data,'{'));
 
           $res = json_decode($re2);
           //dump($res->result->data);
           $result = $res->result;
           var_dump($result);
    }*/
    
}
